package com.mike.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.mike.pojo.T_user;
import com.mike.service.Iservice.t_userService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.dsna.util.images.ValidateCode;

@Controller
public class t_userAction extends ActionSupport implements ModelDriven,RequestAware,ServletResponseAware,SessionAware{

	@Autowired
	private t_userService ts;
	
	private T_user t_user;
	@Override
	public Object getModel() {
		
		return t_user;
	}

	



	public T_user getT_user() {
		return t_user;
	}





	public void setT_user(T_user t_user) {
		this.t_user = t_user;
	}



	private javax.servlet.http.HttpServletResponse response;
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response =response;
	}

	
	private Map<String, Object> requestMap;
	@Override
	public void setRequest(Map<String, Object> requestMap) {
		
		this.requestMap = requestMap;
	}
	
	
	private String remember;

	public String getRemember() {
		return remember;
	}
	public void setRemember(String remember) {
		this.remember = remember;
	}


	
	private Map<String, Object> sessionMap = null;
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
		
	}

	private String valiCode;
	

	public String getValiCode() {
		return valiCode;
	}

	public void setValiCode(String valiCode) {
		this.valiCode = valiCode;
	}





	@Override
	public String execute() throws Exception {
		System.out.println("「検査」に入った。");
		//System.out.println(weapon.getName()+"*******"+weapon.getATK());
		String code = sessionMap.get("code").toString();
		System.out.println("答案"+ code);
		System.out.println("valiCode:" + valiCode);
		String name = t_user.getName();
		String password = t_user.getPassword();
		T_user user = new T_user(name,password);
		int  a = ts.query(user);
		Cookie cookie = new Cookie("name",name);
		
		if(a > 0 && code.equalsIgnoreCase(valiCode)){
			if(remember != null){
				
				response.addCookie(cookie);
				requestMap.put("rem", remember);
			}
			return super.execute();
		}else{
			addActionError("输入错误");
			return "error";
		}
	}














	

}
